package com.hanhai.material.wages.service.impl;

import com.hanhai.material.common.constant.Constant;

import com.hanhai.material.utils.redis.RedisUtil;
import com.hanhai.material.wages.entity.SysRole;
import com.hanhai.material.wages.entity.SysUser;
import com.hanhai.material.wages.mapper.SysUserMapper;
import com.hanhai.material.wages.service.SysRoleService;
import com.hanhai.material.wages.service.SysUserService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.hanhai.material.wages.entity.table.SysUserTableDef.SYS_USER;


/**
 *  服务层实现。
 *
 * @author youchuang
 * @since 2023/10/28 09:29:24
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    RedisUtil redisUtil;

    /*
     * @param username
     * */
    @Override
    public SysUser getByUsername(String username) {
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.where(SYS_USER.USERNAME.eq(username));
        return this.sysUserService.getOne(queryWrapper);
    }

    @Override
    public String getUserAuthorityInfo(String userId) {
        System.out.println("UserId:"+userId);
        SysUser sysUser = this.sysUserService.getById(userId);
        String authority = "";
        // 获取角色编码
        String roleId = sysUser.getRoleId();
        SysRole sysRole = this.sysRoleService.getById(roleId);
        if (sysRole != null) {
            authority = sysRole.getName();
        }
        /*将用户名，角色名，以及有效时间存入Redis*/
//        redisUtil.hset(sysUser.getUsername(), Constant.ROLE_KEY, authority, 3 * 10 * 60);
        redisUtil.hset(sysUser.getUsername(), Constant.ROLE_KEY, authority);

        return authority;
    }
}
